package com.iss.card.view.store;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.util.Log;

import com.iss.card.model.ProductDetailInfo;
import com.iss.card.view.model.StoreDetailModel;

public class StoreUtils {
	private static final String TAG = "StoreUtils";
	/**
	 * ͼƬ����
	 * 
	 * @param busList
	 *            ͼƬ���б�
	 * @param interval
	 *            ͼƬ���
	 * @param disLineNum
	 *            ��ʾ����
	 * @param screenW
	 *            ����
	 */
	public static void computePositions(List<StoreModel> busList, int interval, int disLineNum, int screenW) {
		if (busList == null) {
			return;
		}
		doComputePositions(busList, interval, disLineNum, screenW);
	}

	public static void computeProductPositions(List<ProductDetailInfo> list, int interval, int disLineNum, int screenWidth) {
		if (list == null) {
			return;
		}
		
		if (interval < 0) {
			interval = 0;
		}
		
		final int picWidth = (screenWidth - (4 * interval)) / disLineNum;
		int column = 0;
		int line = 0;
		float bottom = 0;
		int index = 0;
		for (ProductDetailInfo info : list) {
			info.left = 0;
			info.right = 0;
			info.bottom = 0;
			info.top = 0;
		}
		
		for (ProductDetailInfo info : list) {
			if (line == 0) {
				index = column;
			} else {
				index = ((line - 1) * disLineNum) + column;
			}

			bottom = list.get(index).top + list.get(index).bottom;
			info.left = (int) doRound(((column + 1) * interval) + (column * picWidth), 0);
			info.top = (int) doRound(interval + bottom, 0);
			info.right = (int) doRound(picWidth, 0) + info.left;
			info.bottom = (int) doRound(computeShrinkH(picWidth, info.getProductPicW(), info.getProductPicH()), 0);
//			Log.d(TAG, "(" + info.getProductID() + ", " + info.getProductPicUrl()
//							+ ", x=" + info.left + ", y=" + info.top + ", w="
//							+ info.right + ", h=" + info.bottom + ")");
			column++;
			if (column >= disLineNum) {
				column = 0;
				line++;
			}
		}
	}
	
	static private void doComputePositions(List<StoreModel> busList, int interval, int disLineNum, int screenWidth) {
//		Log.i(TAG, "doComputePositions:interval = " + interval + ",disLineNum = " + disLineNum + ",screenW = " + screenWidth);
		if (interval < 0) {
			interval = 0;
		}
		// һ��ͼƬ��ʾ�Ŀ��
		final int picWidth = (screenWidth - (4 * interval)) / disLineNum;
		int column = 0; // ����
		int line = 0; // ����
		float bottom = 0;
		int index = 0;

		for (StoreModel info : busList) {
			// ȥ���һ��
//			Log.i(TAG, info.picWidth + "--" + info.picHeight);
			if (line == 0) {
				index = column;
			} else {
				index = ((line - 1) * disLineNum) + column;
			}
//			Log.d(TAG, "(" + info.getStoreID() + ", " + info.getPicWidth() + ", " + info.getPicHeight() + ")");
			bottom = busList.get(index).top + busList.get(index).bottom;
			info.left = (int) doRound(((column + 1) * interval) + (column * picWidth), 0);
			info.top = (int) doRound(interval + bottom, 0);
			info.right = (int) doRound(picWidth, 0) + info.left;
			info.bottom = (int) doRound(computeShrinkH(picWidth, Integer.parseInt(info.getPicWidth()), Integer.parseInt(info.getPicHeight())), 0);

			column++;
			if (column >= disLineNum) { // ����
				column = 0;
				line++; // ����,��������
			}
		}
	}

	/**
	 * ͨ��ȱ��������ӿ����߶�
	 * 
	 * @param disW
	 *            ��ʾ�Ŀ��
	 * @param w
	 *            ͼƬ���
	 * @param h
	 *            ͼƬ�߶�
	 * @return Integer,�ȱȸ߶�
	 */
	static private float computeShrinkH(float disW, float w, float h) {
		
		return (disW / w) * h;
	}

	/**
	 * ����������ͼƬ
	 * 
	 * @param b
	 *            ��Ҫ���ŵ�bitmap
	 * @param newWidth
	 *            ���ź�Ŀ��
	 * @param newHeight
	 *            ���ź�ĸ߶�
	 * @return Bitmap,������ɵ�bitmap
	 */
	public static Bitmap postScaleForImage(Bitmap b, float newWidth,
			float newHeight) {
		if (b == null) {
			return b;
		}
		int width = b.getWidth();
		int height = b.getHeight();

		float scaleWidth = newWidth / width;
		float scaleHeight = newHeight / height;

		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);
		matrix.reset();

		b = Bitmap.createBitmap(b, 0, 0, width, height, matrix, true);
		return b;
	}

	/**
	 * �ж��Ƿ���Ҫ����
	 * 
	 * @param b
	 * @param newWidth
	 * @return
	 */
	public static boolean isScale(Bitmap b, int newWidth) {
		int width = b.getWidth();

		if (width != newWidth) {
			return true;
		}
		return false;
	}
	
	/**
	 * ���������㷨
	 * 
	 * @param val
	 *            ��Ҫ�����ֵ
	 * @param newScale
	 *            ������λС��
	 * @return double,������ֵ
	 */
	public static double doRound(double val, int newScale) {
		if (newScale < 0) {
			newScale = 0;
		}

		BigDecimal decimal = new BigDecimal(val);
		double douVal = decimal.setScale(newScale, BigDecimal.ROUND_HALF_UP)
				.doubleValue();
		return douVal;
	}
	
	/**
	 * �ֽ�WebService��ȡ��ͼƬ·��
	 * 
	 * @param  ���
	 * @return String[],�ֽ��ֵ
	 */
	public static String[] doAnalysisStringUrl(ArrayList<StoreDetailModel> sdm) {
		
		if (sdm == null) {
			Log.d("kitexy debug", "sdm is null");
			return null;
		}
		int size = sdm.size();
		String [] surl = new String[size];
		for(int i = 0;i<size;i++){
			surl[i] = sdm.get(i).getprdUrl();
			Log.d("kitexy debug", "url ===> " + surl[i]);
		}
		return surl;
	}


}
